package mobileann.mafamily.utils;

import java.util.HashMap;
import java.util.Map;

import mobileann.mafamily.db.model.ClickModuleModel;

import org.json.JSONObject;

import com.mofind.android.base.L;
import com.mofind.android.net.HttpUtils;
import com.mofind.android.net.JSONParser;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

public class MAActivityUtils {
	private static MAActivityUtils _instance;
	private String TAG = "MAActivityUtils";

	public synchronized static MAActivityUtils getInstance() {
		if (_instance == null) {
			_instance = new MAActivityUtils();
		}
		return _instance;

	}

	public Map<String, String> getMAActivityPara(Context c, String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "lottery_draw");
		map.put("uid", uid);
		map.put("data", MAActivityUtils.getInstance().getMAActivity3DES(c));
		return map;
	}

	private String getMAActivity3DES(Context c) {
		String id = SPUtils.getTagStringValue("MoannActivityId");
		String channel = SPUtils.getVersionChannel();
		String phoneNum = SPUtils.getUID();
		String uuid = PhoneInfoUtils.getMyUUID(c);
		String imei = PhoneInfoUtils.getIMEI(c);
		String version = PhoneInfoUtils.getVersionCode(c);
		String data = "{\"id\":\"" + id + "\",\"channel\":\"" + channel
				+ "\",\"version\":\"" + version + "\",\"phone\":\"" + phoneNum
				+ "\",\"imei\":\"" + imei + "\",\"uuid\":\"" + uuid + "\"}";
		// return TripleDESUtil.CBCEncode(SPUtils.getUID2(), SPUtils.getURAD(),
		// data);
		return TripleDESUtil.CBCEncode(SPUtils.getMySelf(SPUtils.MYUID),
				SPUtils.getMySelf(SPUtils.MYURAD), data);
	}

	/*
	 * 活动上传
	 */
	public void doMAActivity2Server(final Context c,
			final AwardListener listener) {
		HttpUtils.startHttpPost(
				URLUtils.URL_GROWTH_UPLOAD,
				MAActivityUtils.getInstance().getMAActivityPara(c, SPUtils.getMySelf(SPUtils.MYUID)),
				new HttpUtils.HttpResultCallback() {
					@Override
					public void result(String url, boolean isResult, String jsonStr) {
						if (isResult) {
							// String result = TripleDESUtil.getJsonObjStr(
							// SPUtils.getUID2(), SPUtils.getURAD(),
							// jsonStr);
							String result = TripleDESUtil.getJsonObjStr(
									SPUtils.getMySelf(SPUtils.MYUID),
									SPUtils.getMySelf(SPUtils.MYURAD), jsonStr);
							JSONObject jo = JSONParser.getJSONObject(result);
							String status = JSONParser.getString(jo, "status");
							if (!TextUtils.isEmpty(status)) {
								if ("true".equals(status)) {
									JSONObject awardResult = JSONParser.getJSONObject(jo, "result");
									String award = JSONParser.getString(awardResult, "msg");
									listener.setAward(award);
								} else {
									L.e(TAG, "上传数据失败");
								}
							} else {
								L.e(TAG, "ERROR");
							}
						}
					}
				});
	}

	public interface AwardListener {
		void setAward(String award);
	}
}
